// 去重的写法
const arr = ['1', 1, 1, 2, 3, 4, 4, '1', 7, 8, 9]
//1---------------------------------------使用Set
function quchong1(arr) {

    return Array.from(new Set(arr))
}

// 2----------------------filter和indexof组合
// 数组中每个元素在数组的中第一个位置和他当前索引相同
function quchong2(arr) {
    arr.filter((item, index, arr) => {
        return arr.indexof(item, 0) === index
    })

}

// 3------------------------使用map
function quchong3(arr) {
    let map = new Map()
    let newArr = []
    arr.formEach((item, index, arr) => {
        if (!map.has(item)) {
            map.set(item, item)
            newArr.push(item)
        }
    })

    return newArr
}

// 4------------------------使用数组双循环和splice()
function quchong4(arr) {
    let len = arr.length
    for (let i = 0; i < len; i++) {
        for (let j = i + 1; j < len; j++) {
            if (arr[i] === arr[j]) {
                arr.splice(j, 1)
                len--
                j--
            }
        }
    }
}

// 5------------------------使用indexof判断
function quchong4(arr) {
    let newArr = []
    arr.forEach(item => {
        if (newArr.indexOf(item) === -1) {
            newArr.push(item)
        }
    })
    return newArr
}


// 深拷贝

function deepClone(targetData) {

    let newData
    if(typeof targetData === 'object') {

        if(Array.isArray(targetData)) { //数组
            newData = []
            for(let i in targetData) {
                newData.push(deepClone(targetData[i]))  
            }
        }else if(targetData == null) {
            newData = null
        }else if(targetData.constructor === RegExp){
            newData = targetData
        }else {
            newData = {}
            for(let i in targetData) {
                newData[i] = deepClone(targetData[i])
            }
        }
    }else {
        newData = targetData
    }

    return newData
}


// 防抖    节流
function debounce(fn,delay=300) {
    let timer
    return function() {
        if(timer) {
            clearTimeout(timer)
        }

        timer = setTimeout(()=> {
            fn.apply(this,arguments)
        },delay)

    }

}

function throttle(fn,delay) {
    let flag = true
    return function() {
        if(!flag) return
        flag = true
        setTimeout(()=>{
            fn.apply()
            flag = false
        },delay)


    }
}


Function.prototype.call = function(context= window,...args) {
    if(typeof this !== 'function') {
        throw new TypeError('type is not function')
    }

    let fn = Symbol('fn')
    context[fn] = this
    let res = context[fn](...args)
    delete context[fn]
    return res

}